/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.pod.service.impl;

import me.zhengjie.modules.cspimp.pod.domain.Rtl;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.pod.repository.RtlRepository;
import me.zhengjie.modules.cspimp.pod.service.RtlService;
import me.zhengjie.modules.cspimp.pod.service.dto.RtlDto;
import me.zhengjie.modules.cspimp.pod.service.dto.RtlQueryCriteria;
import me.zhengjie.modules.cspimp.pod.service.mapstruct.RtlMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RtlServiceImpl implements RtlService {

    private final RtlRepository rtlRepository;
    private final RtlMapper rtlMapper;

    @Override
    public Map<String,Object> queryAll(RtlQueryCriteria criteria, Pageable pageable){
        Page<Rtl> page = rtlRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(rtlMapper::toDto));
    }

    @Override
    public List<RtlDto> queryAll(RtlQueryCriteria criteria){
        return rtlMapper.toDto(rtlRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public RtlDto findById(Long id) {
        Rtl rtl = rtlRepository.findById(id).orElseGet(Rtl::new);
        ValidationUtil.isNull(rtl.getId(),"Rtl","id",id);
        return rtlMapper.toDto(rtl);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RtlDto create(Rtl resources) {
        return rtlMapper.toDto(rtlRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Rtl resources) {
        Rtl rtl = rtlRepository.findById(resources.getId()).orElseGet(Rtl::new);
        ValidationUtil.isNull( rtl.getId(),"Rtl","id",resources.getId());
        rtl.copy(resources);
        rtlRepository.save(rtl);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            rtlRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RtlDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RtlDto rtl : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", rtl.getCreateTime());
            map.put("更新时间", rtl.getUpdateTime());
            map.put("创建者", rtl.getCreateBy());
            map.put("更新者", rtl.getUpdateBy());
            map.put("身份", rtl.getIdentity());
            map.put("姓名", rtl.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}